package example1;

import hmm.Hmm;
import hmm.HmmException;
import hmm.MarkovModel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Example1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// read observations from user
		String observationString = null;
		System.out.println("please enter a set of coin flips(H\\T):");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			observationString = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int[] observation = new int[observationString.length()];
		for(int i = 0; i < observation.length; i++) {
			if(observationString.charAt(i) == 'h' || observationString.charAt(i) == 'H') {
				observation[i] = 0;
			} else if(observationString.charAt(i) == 't' || observationString.charAt(i) == 'T') {
				observation[i] = 1;
			} else {
				System.err.println("Illigal input!!!");
				return;
			}
		}
		// preparing the model
		double[] pi = new double[2];
		pi[0] = 0.5;
		pi[1] = 0.5;
		double p;
		try {
			MarkovModel model = new MarkovModel(2, 2, new ProbEx1(observation), pi, observation.length);
			Hmm res = new Hmm(model);
			p = res.forwardAlgorithm();
			System.out.println("the probability for this observation is: "+p);
		} catch (HmmException e) {
			e.printStackTrace();
		}
	}

}
